/***************************************************************************
 *   Copyright (C) 2007 by Ziman & Xloverin                                *
 *   ziman@centrum.sk, xloverin@gmail.com                                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/


#include "prefslog.h"
#include "log.h"

void PrefsLog::on_leFileName_textChanged()
{
	lLogPreview->setText(Log::format(leFileName->text(), "Main"));
}

void PrefsLog::on_leTimestamp_textChanged()
{
	lTimestampPreview->setText(Log::format(leTimestamp->text(), "Main"));
}

void PrefsLog::on_rbSingle_clicked()
{
	leFileName->setText("log/atlantis");
}

void PrefsLog::on_rbQuery_clicked()
{
	leFileName->setText("log/%q");
}

void PrefsLog::on_rbSingleMonth_clicked()
{
	leFileName->setText("log/%y_%m");
}

void PrefsLog::on_rbQueryMonth_clicked()
{
	leFileName->setText("log/%q_%y_%m");
}

void PrefsLog::on_cbHtml_toggled(bool checked)
{
	// if (checked) cbPlainHtml->setChecked(false);
}

void PrefsLog::on_cbPlainHtml_toggled(bool checked)
{
	// if (checked) cbHtml->setChecked(false);
}

PrefsLog::Prefs PrefsLog::prefs()
{
	Prefs prefs;

	prefs.format = 0;
	if (cbPlaintext->isChecked()) prefs.format |= Plaintext;
	if (cbVt100->isChecked()) prefs.format |= Vt100;
	if (cbPlainHtml->isChecked()) prefs.format |= PlainHtml;
	if (cbHtml->isChecked()) prefs.format |= Html;
	qDebug("Saving prefs.format as 0x%04x.\n", prefs.format);

	prefs.fileName = leFileName->text();
	prefs.timestamp = leTimestamp->text();
	prefs.timestampEnabled = (cbTimestampEnabled->checkState() == Qt::Checked);

	if (rbSingle->isChecked()) prefs.fnFormatSelected = 0;
	if (rbQuery->isChecked()) prefs.fnFormatSelected = 1;
	if (rbSingleMonth->isChecked()) prefs.fnFormatSelected = 2;
	if (rbQueryMonth->isChecked()) prefs.fnFormatSelected = 3;
	if (rbCustom->isChecked()) prefs.fnFormatSelected = 4;

	return prefs;
}

void PrefsLog::setPrefs(PrefsLog::Prefs prefs)
{
	qDebug("Loading prefs.format as 0x%04x.\n", prefs.format);
	cbPlaintext->setChecked((prefs.format & Plaintext) != 0);
	cbVt100->setChecked((prefs.format & Vt100) != 0);
	cbPlainHtml->setChecked((prefs.format & PlainHtml) != 0);
	cbHtml->setChecked((prefs.format & Html) != 0);

	leFileName->setText(prefs.fileName);
	leTimestamp->setText(prefs.timestamp);
	cbTimestampEnabled->setCheckState(prefs.timestampEnabled ? Qt::Checked : Qt::Unchecked);

	switch (prefs.fnFormatSelected) {
		case 0:	rbSingle->click();	 	break;
		case 1:	rbQuery->click();		break;
		case 2:	rbSingleMonth->click();	break;
		case 3:	rbQueryMonth->click();	break;
		case 4:	rbCustom->click();		break;
		default: break;
	}

	on_leFileName_textChanged();
	on_leTimestamp_textChanged();
}

PrefsLog::PrefsLog ( QWidget* parent, Qt::WFlags fl )
		: QWidget ( parent, fl ), Ui::prefsLog()
{
	setupUi ( this );
}

PrefsLog::~PrefsLog()
{
}


